//
//  TestPageContent2VC.swift
//  CooperSwift_Example
//
//  Created by 计海峰 on 2021/6/10.
//  Copyright © 2021 CocoaPods. All rights reserved.
//

import Foundation
import RxSwift
import JXSegmentedView
import CooperSwift


class TestCollectionCellModel: NSObject, CollectionCellConfigProtocol, BindKeyProtocol {
  
    
    var model: Any?
    
    required init(_ model: Any?) {
        self.model = model
    }
    
    static func initWith(_ model: Any, size: CGSize = CGSize(120, 120)) -> TestCollectionCellModel{
        var info = TestCollectionCellModel(model)
        info.cellSize = size
        return info
    }
    
    func cellCls() -> UICollectionViewCell.Type {
        if model is String && "测试2" == model as! String {
            return TestAutoCollection2Cell.self
        }
        return TestAutoCollectionCell.self
    }
    
}


class TestPageContent3VM: NSObject, CollectionViewModelProtocol, UICollectionViewDelegateFlowLayout, RefreshControllable {

    func configDown() {
        print("TestPageContent3VM configDown")
        addFirstSectionItems([
            TestCollectionCellModel.initWith("测试1", size: CGSize(80, 120)).addTargetKey(bindKey()),
            TestCollectionCellModel.initWith("测试2").addTargetKey(bindKey()),
            TestCollectionCellModel.initWith("测试1").addTargetKey(bindKey()),
            TestCollectionCellModel.initWith("测试2").addTargetKey(bindKey()),
            TestCollectionCellModel.initWith("测试1", size: CGSize(80, 120)).addTargetKey(bindKey()),
            TestCollectionCellModel.initWith("测试2").addTargetKey(bindKey()),
            TestCollectionCellModel.initWith("测试1").addTargetKey(bindKey()),
            TestCollectionCellModel.initWith("测试2").addTargetKey(bindKey()),
        ])
        
    }
    
//    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
//        return vm_collectionView(collectionView, layout: collectionViewLayout, sizeForItemAt: indexPath)
//    }
    
    @objc func sumbit()  {
        log.debug("sumbit")
    }
    
    func refresh() {
        log.debug("TestPageContent3VM refresh")
        PublicMethod.delay(2) {
            self.rx.refreshStatus.onNext(.endHeaderRefresh)
        }
    }
    
    
//    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
//        return vm_collectionView(collectionView, layout: collectionViewLayout, sizeForItemAt: indexPath)
//    }
        
}

class TestPageContent3VC: UIViewController, CollectionEnableProtocol, Refreshable, WaterfallFlowLayoutDelegate {
    
    let disposeBag = DisposeBag()
    let vm = ViewModelFactory.collectionVM(TestPageContent3VM.self)
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        rx.updateRefresh(vm, scrollview: collectionView, bag: disposeBag, refreshConfig: .header)
        
        
        let layout = WaterfallFlowLayout.init(lineSpacing: 8, columnSpacing: 8, sectionInsets: UIEdgeInsets.space(8.0))
        layout.delegate = self
        configAll(vm, disposeBag: disposeBag, flowLayout: layout)
    }
    
    func columnOfWaterFall(_ collectionView: UICollectionView) -> Int {
        return 3
    }
    
    func waterFall(_ collectionView: UICollectionView, layout waterFallLayout: WaterfallFlowLayout, heightForItemAt indexPath: IndexPath) -> CGFloat {
//        let item = vm.item(indexPath: indexPath)
        if indexPath.row % 2 == 0 {
            return 120
        }
        return 80
    }
    
    
}
